#include "cpu/exec/template-start.h"

#define instr add

static void do_execute(){
    DATA_TYPE res = op_dest->val + op_src->val;     //操作数相加
    OPERAND_W(op_dest, res);        //结果写会目标操作数
    //设置进位标志CF
    cpu.eflags.CF = (res < op_dest->val);
    //设置符号标志SF
    cpu.eflags.SF = res >> (DATA_BYTE * 8 - 1);
    //计算操作数符号位
    int a = op_dest->val >> (DATA_BYTE * 8 - 1);    //目标操作数
    int b = op_src->val >> (DATA_BYTE * 8 - 1);     //源操作数
    //设置溢出标志位OF
    cpu.eflags.OF = (a == b) && (b != cpu.eflags.SF);       //符号位相同但结果符号位不同
    //设置零标志ZF
    cpu.eflags.ZF = !res;
    res ^= res >> 4;
    res ^= res >> 2;
    res ^= res >> 1;
    cpu.eflags.PF = !(res &1);
    //设置辅助进位标志AF:若低四位相加产生进位设为1
    if(((op_dest->val & 0xf) + (op_src->val & 0xf)) >> 4) cpu.eflags.AF = 1;
    else cpu.eflags.AF = 0;
    print_asm_template2();
}

make_instr_helper(i2a)      //立即数到累加器
make_instr_helper(i2rm)     //立即数到内存
#if DATA_BYTE == 2 || DATA_BYTE == 4
make_instr_helper(si2rm)        //符号立即数到内存
#endif
make_instr_helper(r2rm)         //寄存器到内存
make_instr_helper(rm2r)         //内存到寄存器

#include "cpu/exec/template-end.h"